<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
  <head>
	<title>用户新添</title>
	<link rel="stylesheet" href="./css/list.css" type="text/css" />
  </head>
  <body>
	<%@ include file="./header.jsp" %>
    <h3>用户新添</h3>

	<form action="registerDoServlet" method="post">
	<table width="500" align="center" class="table_border table_padding10">
		<tr class="tr_header">
			<td>项目</td>
			<td>内容</td>
		</tr>
		<tr>
			<td>用户名</td>
			<td class="left">
				<input type="text" name="username" maxlength="45">&ensp;
				<span class="msg">*</span>
			</td>
		</tr>
		<tr>
			<td>密码</td>
			<td class="left">
				<input type="password" name="password" maxlength="45">&ensp;
				<span class="msg">*</span>
			</td>
		</tr>
		<tr>
			<td>确认密码</td>
			<td class="left">
				<input type="password" name="password2" maxlength="45">&ensp;
				<span class="msg">*</span>
			</td>
		</tr>
		<tr height="40">
			<td colspan="2" style="padding-left:30px; font-size:small;">			
				<input type="submit" value="提交" onclick="return ajax();">			
				<!-- <input type="button" value="测试js" onclick="return ajax();"> -->
				<!-- 当js代码测试通过之后，再将此button注释掉 -->
			</td>
		</tr>
	</table>
	</form>

	<div id="msg" class="msg">${ msg }</div>
	<br>
	<a href="index.jsp">返回登录</a>
	
	<%@ include file="./footer.jsp" %>
  </body>
<script type="text/javascript">	

function ajax() {
	var username 	= document.getElementsByName("username")[0].value;	//根据name获取输入的值
	var password 	= document.getElementsByName("password")[0].value;
	var password2 	= document.getElementsByName("password2")[0].value;
	username 	= encodeURIComponent(username);							//参数URI编码
 	//password 	= encodeURIComponent(password);
 	//password2 = encodeURIComponent(password2);


	var url = document.getElementsByTagName("form")[0].action;			//根据标签名获取表单的action
    url += "?username=" + username;										//最前面是符号“?”
    url += "&password=" + password;										//最前面是符号“&”（之前有参数时）
	url += "&password2=" + password2;

	var xhr = new XMLHttpRequest();     						//创建XMLHttpRequest对象	
	xhr.open('post', url, true);								//post方法，网址，异步
	xhr.send();         										//发送请求	
	xhr.onreadystatechange = function () {						//状态更改时执行
	    if (xhr.readyState == 4 && xhr.status == 200) {
	        setMsg(xhr.responseText); 							//显示成功返回的内容
	    } else {
	        //setMsg("XMLHttpRequest对象创建失败!");				//未成功
	    }
	}
	
	return false;		
}
  
function setMsg(msg) {
	if (msg.substring(0, 10) == "@Redirect:") {			//截取前10个字符。表示需网页转向
		var url = msg.substring(10); 					//截取第11个字符及之后的所有字符
		window.location.href = url;						//网页转向
		return;
	}
	
	document.getElementById("msg").innerHTML = msg;		//显示内容
}
</script>
</html>
